//
//  MyWebViewVC.m
//  truck_Driver_1_0
//
//  Created by 李志权 on 16/5/16.
//  Copyright © 2016年 李志权. All rights reserved.
//

#import "MyWebViewVC.h"

#import <WebKit/WebKit.h>

@interface MyWebViewVC ()<WKUIDelegate,WKNavigationDelegate>
{
    NSString *_url;

}

@property (nonatomic, strong) WKWebView *wkWebView;
@property (nonatomic, strong) UIProgressView *progressView;
@end

@implementation MyWebViewVC
- (id)initWithUrl:(NSString *)url
{
    self = [super init];
    _url = url;
    return self;
}


- (void)viewDidLoad {//
    
    [super viewDidLoad];
  
    self.wkWebView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0,Width, Highly)];
    self.wkWebView.UIDelegate = self;
    self.wkWebView.navigationDelegate = self;
    [self.view addSubview:self.wkWebView];
    
    //进度条初始化
    self.progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(0, 62, [[UIScreen mainScreen] bounds].size.width, 2)];
    self.progressView.backgroundColor = [UIColor blueColor];
    //设置进度条的高度，下面这句代码表示进度条的宽度变为原来的1倍，高度变为原来的1.5倍.
    self.progressView.transform = CGAffineTransformMakeScale(1.0f, 1.5f);
    [self.navigationController.view addSubview:self.progressView];
    
    
    
    [self.wkWebView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
    [self.wkWebView addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:NULL];
    
    UIButton *goBackBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    goBackBtn.frame = CGRectMake(0, 0, 9, 18);
    [goBackBtn setBackgroundImage:[UIImage imageNamed:@"登录_left-arrow"] forState:UIControlStateNormal];
    [goBackBtn addTarget:self action:@selector(goBackBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *goBack = [[UIBarButtonItem alloc]initWithCustomView:goBackBtn];
    
    UIBarButtonItem *shutBar = [[UIBarButtonItem alloc]initWithTitle:@"关闭" style:UIBarButtonItemStylePlain target:self action:@selector(shutBarClick)];
    
    [self.navigationItem setLeftBarButtonItems:@[goBack,shutBar] animated:YES];
    
    [self setupWebviews];
}
- (void)goBackBtnClick
{
    if ([self.wkWebView goBack]) {
        [self.wkWebView goBack];
    }
    else
    {
        [self.navigationController popViewControllerAnimated:YES];
    }
}
- (void)shutBarClick
{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - 设置web view
- (void)setupWebviews
{
    [self loadURL];
}

- (void)loadURL
{
    NSURLRequest *requestURL = [NSURLRequest requestWithURL:[NSURL URLWithString:_url]];//加载网络网页
    [self.wkWebView loadRequest:requestURL];//加载网页
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
    if ([keyPath isEqualToString:@"estimatedProgress"]) {
        self.progressView.progress = self.wkWebView.estimatedProgress;
        if (self.progressView.progress == 1) {
            /*
             *添加一个简单的动画，将progressView的Height变为1.4倍，在开始加载网页的代理中会恢复为1.5倍
             *动画时长0.25s，延时0.3s后开始动画
             *动画结束后将progressView隐藏
             */
            __weak typeof (self)weakSelf = self;
            [UIView animateWithDuration:0.25f delay:0.3f options:UIViewAnimationOptionCurveEaseOut animations:^{
                weakSelf.progressView.transform = CGAffineTransformMakeScale(1.0f, 1.4f);
            } completion:^(BOOL finished) {
                weakSelf.progressView.hidden = YES;
                
            }];
        }
    }
    else if ([keyPath isEqualToString:@"title"])
    {
         self.navigationItem.title = self.wkWebView.title;
    }
    else{
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
}
//开始加载
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
    NSLog(@"开始加载网页");
    //开始加载网页时展示出progressView
    self.progressView.hidden = NO;
    //开始加载网页的时候将progressView的Height恢复为1.5倍
    self.progressView.transform = CGAffineTransformMakeScale(1.0f, 1.5f);
    //防止progressView被网页挡住
    [self.view bringSubviewToFront:self.progressView];
}

//加载完成
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
    NSLog(@"加载完成");
    //加载完成后隐藏progressView
    self.progressView.hidden = YES;
}

//加载失败
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error {
    NSLog(@"加载失败");
    //加载失败同样需要隐藏progressView
    self.progressView.hidden = YES;
}
- (void)dealloc {
    [self.wkWebView removeObserver:self forKeyPath:@"estimatedProgress"];
    [self.wkWebView removeObserver:self forKeyPath:@"title"];
}
@end
